package com.moos.example.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.moos.example.R;
import com.moos.example.bean.DiscussDetailBean;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class RecyclerBaseAdapter extends RecyclerView.Adapter<RecyclerBaseAdapter.ViewHolder> {

    private Context context;

    //the data
    private ArrayList<DiscussDetailBean> mData;

    private OnItemClickListener mOnItemClickListener = null;


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

    //define interface
    public interface OnItemClickListener {
        void onItemClick(int position);
    }


    public void updateData(DiscussDetailBean data) {
        if (data != null) {
            mData.add(data);
            notifyDataSetChanged();
        } else {
            throw new IllegalArgumentException("发帖数据为空!");
        }
    }

    public RecyclerBaseAdapter(Context context, ArrayList<DiscussDetailBean> mData) {
        this.context = context;
        this.mData = mData;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.discuss_list, parent, false));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        DiscussDetailBean detailBean = mData.get(position);

        holder.page_userName.setText(detailBean.getNickName());
        holder.page_time.setText(detailBean.getCreateDate());
        holder.page_title.setText(detailBean.getTitle());
        holder.page_story.setText(detailBean.getContent());
        holder.page_reply_count.setText(detailBean.getReplyCount() + "回帖");
        Glide.with(context).load(detailBean.getUserLogo())
                // ???
                .diskCacheStrategy(DiskCacheStrategy.DATA)
                .error(R.drawable.user_other)
                .centerCrop()
                .into(holder.page_userLogo);

        //实现点击效果
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    /**
     * 视图holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {


        CircleImageView page_userLogo;
        TextView page_userName;
        TextView page_time;
        ImageView page_focus;
        TextView page_title;
        TextView page_story;
        TextView page_reply_count;


        ViewHolder(View itemView) {
            super(itemView);
            page_focus = itemView.findViewById(R.id.page_focus);
            page_time = itemView.findViewById(R.id.page_time);
            page_title = itemView.findViewById(R.id.page_title);
            page_story = itemView.findViewById(R.id.page_story);
            page_userName = itemView.findViewById(R.id.page_userName);
            page_userLogo = itemView.findViewById(R.id.page_userLogo);
            page_reply_count = itemView.findViewById(R.id.page_reply_count);
        }
    }
}



